package com.zebra.register;

import com.zebra.remoting.config.RegisterServerInfo;
import com.zebra.remoting.connection.Connection;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * 简单处理 需要优化
 */
public class RegisterConfigInfoManager {

    private static RegisterConfigInfoManager poolManager = new RegisterConfigInfoManager();

    private RegisterServerInfo registerServerInfo;


    private ConcurrentHashMap<Integer, Connection> tmsServers = new ConcurrentHashMap<>(4);

    /* 多个网关 */
    private ConcurrentHashMap<Integer, Connection> gateways = new ConcurrentHashMap<>(4);

    private RegisterConfigInfoManager(){
    }

    public static RegisterConfigInfoManager getInstance() {
        return poolManager;
    }

    public RegisterServerInfo getRegisterServerInfo() {
        return registerServerInfo;
    }

    public void setRegisterServerInfo(RegisterServerInfo registerServerInfo) {
        this.registerServerInfo = registerServerInfo;
    }

    public void registerTms(Connection connection){
        tmsServers.put(connection.getServerId(), connection);
    }

    public void registerGateways(Connection connection){
        gateways.put(connection.getServerId(), connection);
    }

    public void removeTms(int tmsSid){
        tmsServers.remove(tmsSid);
    }

    public void removeGateway(int gwSid){
        gateways.remove(gwSid);
    }

    public Connection getGwConnection(int gwSid){
        return gateways.get(gwSid);
    }
    public Connection getTmsConnection(int tmsSid){
        return tmsServers.get(tmsSid);
    }

    public List<Connection> getTmsServers(){
        return this.tmsServers.values().stream().filter(c -> c.isFine()).collect(Collectors.toList());
    }

    public List<Connection> getGateways(){
        return this.gateways.values().stream().filter(c -> c.isFine()).collect(Collectors.toList());
    }


}
